package com.weimin.springbootgrpcserver.grpc.service;


import com.weimin.proto.BaseGrpcServiceGrpc;
import com.weimin.proto.BaseMessage;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import net.devh.boot.grpc.server.service.GrpcService;

/**
 * grpc 方法实现类
 */
@GrpcService
@Slf4j
public class MessageGrpcService extends BaseGrpcServiceGrpc.BaseGrpcServiceImplBase {

    @Override
    public void commonMethod(BaseMessage.Message request, StreamObserver<BaseMessage.Message> responseObserver) {
        BaseMessage.MessageRequest messageRequest = request.getRequest();
        String id = messageRequest.getId();
        String data = messageRequest.getData();
        log.info("commonMethod 请求参数->类型 {} 请求数据 {}", request.getPackType(), data);
        // 回复数据
        BaseMessage.MessageResponse build = BaseMessage.MessageResponse.newBuilder().setCode(id).setData("服务端回复数据:" + data).build();
        responseObserver.onNext(BaseMessage.Message.newBuilder()
                .setPackType(BaseMessage.Message.PackType.RESPONSE)
                .setResponse(build).build());
        // 方法结束
        responseObserver.onCompleted();
    }

    @Override
    public void serviceEndStreamMethod(BaseMessage.Message request, StreamObserver<BaseMessage.Message> responseObserver) {
        BaseMessage.MessageRequest messageRequest = request.getRequest();
        String id = messageRequest.getId();
        String data = messageRequest.getData();
        log.info("serviceEndStreamMethod 请求参数->类型 {} 请求数据 {}", request.getPackType(), data);
        data = "->" + data;
        for (int i = 1; i <= 10; i++) {
            // 回复数据
            BaseMessage.MessageResponse build = BaseMessage.MessageResponse.newBuilder().setCode(id).setData("服务端回复数据:" + i + data).build();
            responseObserver.onNext(BaseMessage.Message.newBuilder()
                    .setPackType(BaseMessage.Message.PackType.RESPONSE)
                    .setResponse(build).build());
        }
        // 方法结束
        responseObserver.onCompleted();
    }

    @Override
    public StreamObserver<BaseMessage.Message> clientStreamMethod(StreamObserver<BaseMessage.Message> responseObserver) {
        return new StreamObserver<BaseMessage.Message>() {

            @Override
            public void onNext(BaseMessage.Message request) {
                BaseMessage.MessageRequest messageRequest = request.getRequest();
                String id = messageRequest.getId();
                String data = messageRequest.getData();
                log.info("clientStreamMethod 请求参数->类型 {} 请求ID {} 请求数据 {}", request.getPackType(), id, data);
            }

            @Override
            public void onError(Throwable t) {
                log.error("clientStreamMethod 异常", t);
            }

            @Override
            public void onCompleted() {
                // 回复数据
                BaseMessage.MessageResponse build = BaseMessage.MessageResponse.newBuilder()
                        .setCode(String.valueOf(System.currentTimeMillis()))
                        .setData("clientStreamMethod 服务端回复").build();
                responseObserver.onNext(BaseMessage.Message.newBuilder()
                        .setPackType(BaseMessage.Message.PackType.RESPONSE)
                        .setResponse(build).build());
                // 方法结束
                responseObserver.onCompleted();
            }
        };
    }

    @Override
    public StreamObserver<BaseMessage.Message> bidirectionalFlowMethod(StreamObserver<BaseMessage.Message> responseObserver) {
        return new StreamObserver<BaseMessage.Message>() {
            @Override
            public void onNext(BaseMessage.Message request) {
                BaseMessage.MessageRequest messageRequest = request.getRequest();
                String id = messageRequest.getId();
                String data = messageRequest.getData();
                log.info("bidirectionalFlowMethod 请求参数->类型 {} 请求ID {} 请求数据 {}", request.getPackType(), id, data);
                // 回复数据
                BaseMessage.MessageResponse build = BaseMessage.MessageResponse.newBuilder().setCode(id).setData("服务端回复数据:" + data).build();
                responseObserver.onNext(BaseMessage.Message.newBuilder()
                        .setPackType(BaseMessage.Message.PackType.RESPONSE)
                        .setResponse(build).build());
            }

            @Override
            public void onError(Throwable t) {
                log.error("异常", t);
            }

            @Override
            public void onCompleted() {
                // 方法结束
                responseObserver.onCompleted();
            }
        };
    }
}
